package com.ng.biz.controller;

import java.util.Date;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.ng.biz.model.domain. DataErrorLog;
import com.ng.common.annotation.TokenRequired;
import com.ng.common.annotation.Tx;
import com.ng.common.model.response.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "数据错误日志管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("/biz/data/error")
@Slf4j
public class DataErrorLogController {
	
	@Operation(summary = "分页获取数据源列表")
	@Parameters({ 
		@Parameter(name = "keywords", description = "关键字", required = false),
		@Parameter(name = "type", description = "日志类型(MQTT|HTTP|OLTP)", required = false),
		@Parameter(name = "level", description = "日志级别", required = false),
		@Parameter(name = "status", description = "状态(0|1)", required = false),
		@Parameter(name = "startTime", description = "开始时间", required = false),
		@Parameter(name = "endTime", description = "结束时间", required = false),
		@Parameter(name = "cur", description = "当前页码", required = true),
		@Parameter(name = "limit", description = "每页数据量", required = true)})
	@TokenRequired
	@GetMapping(value = { "/pages" }, produces = "application/json; charset=utf-8")
	public R<Page<DataErrorLog>> pages(HttpServletRequest request,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "type", required = false, defaultValue = "") String type,
			@RequestParam(name = "level", required = false, defaultValue = "") String level,
			@RequestParam(name = "status", required = false, defaultValue = "") String status,
			@RequestParam(name = "startTime", required = false, defaultValue = "") String startTime,
			@RequestParam(name = "endTime", required = false, defaultValue = "") String endTime,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {
		String sqlExceptSelect = " from " +  DataErrorLog.TABLE_NAME + "  a WHERE 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " and (a.title like '%" + keywords + "%' ) ";
		}
		if (!StrKit.isBlank(type)) {
			sqlExceptSelect += " and (a.type = '" + type + "' ) ";
		}
		if (!StrKit.isBlank(level)) {
			sqlExceptSelect += " and (a.level = '" + level + "' ) ";
		}
		if (!StrKit.isBlank(status)) {
			sqlExceptSelect += " and (a.level = '" + status + "' ) ";
		}
		if (!StrKit.isBlank(startTime) && !StrKit.isBlank(endTime)) {
			sqlExceptSelect += " AND ( a.START_TIME BETWEEN '" + startTime + "' AND '" + endTime + "'  ) ";
		}
		sqlExceptSelect += "  ORDER BY a.START_TIME desc ";
		Page< DataErrorLog> page= DataErrorLog.dao.paginate(cur, limit, "select a.* ", sqlExceptSelect);
		return R.ok(page);
	}
	
	@Operation(summary = "标记关闭")
	@Parameters({ @Parameter(name = "id", description = "日志Id", required = true) })
	@TokenRequired
	@Tx
	@RequestMapping(value = "/{id}/close", method = RequestMethod.POST)
	public R<String> saveDbapiGroup(HttpServletRequest request, @PathVariable("id") String id) {
		try {
			DataErrorLog dataErrorLog=new DataErrorLog();
			dataErrorLog.setId(id);
			dataErrorLog.setStopTime(new Date(System.currentTimeMillis()));
			dataErrorLog.setStatus("1");
			if (dataErrorLog.update()) {
				return R.ok();
			} else {
				return R.failed("失败！");
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}
}
